package com.zoro.barn.inner.service.system.user.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.zoro.barn.api.system.user.UserInfoService;
import com.zoro.barn.commons.tools.json.JsonUtil;
import com.zoro.barn.dto.system.BarnUserInfoDto;
import com.zoro.barn.dto.system.userinfodto.GetUserInfosReq;
import com.zoro.barn.inner.service.system.user.entity.UserEntity;
import com.zoro.barn.inner.service.system.user.service.UserService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;

/**
 * @author zhaoxingwu
 */
@RestController
@RequestMapping("user-info")
public class UserController implements UserInfoService {

    private final UserService userService;

    public UserController(UserService userService) {
        this.userService = userService;
    }

    @Override
    public List<BarnUserInfoDto> getUserInfos(GetUserInfosReq req) throws JsonProcessingException {
        if (req.getIds() == null || req.getIds().length == 0) {
            return null;
        }
        List<UserEntity> userEntities = userService.getUsers(Arrays.asList(req.getIds()));
        return JsonUtil.toJavaList(JsonUtil.toString(userEntities), BarnUserInfoDto.class);
    }
}
